package trail

import (
	"encoding/json"
	"fmt"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"regexp"
)

type JSON map[string]string

// 读取文件
func readFileTran(fileName string) string {
	path, _ := os.Getwd()
	file, err := os.OpenFile(path+"\\"+fileName, os.O_RDONLY, 0755)
	if err != nil {
		panic(err)
	}

	defer func() { // 关闭文件
		err = file.Close()
		if err != nil {
			panic(err)
		}
	}()

	stat, err := file.Stat()
	if err != nil {
		panic(err)
	}

	text := make([]byte, stat.Size())
	n, err := file.Read(text)
	if err != nil {
		panic(err)
	}
	s := string(text[:n])
	return s
}

// 写文件
func writeFileTran(fileName string, content string) bool {
	path, _ := os.Getwd()
	file, err := os.OpenFile(path+"\\"+fileName, os.O_RDWR, 0755)
	if err != nil {
		panic(err)
	}
	// 记得关闭文件句柄
	defer func() {
		err = file.Close()
		if err != nil {
			panic(err)
		}
	}()

	n, err := file.WriteString(content)
	if err != nil {
		panic(err)
	}

	err = file.Sync() // 同步到硬盘
	if err != nil {
		panic(err)
	}

	return n == len(content)
}

// 替换翻译文字，返回新的内容
func reStr(str string) string {
	var res []byte
	newStr := str
	translateJson := analysisJson()
	for k, v := range translateJson {
		c, err := regexp.Compile(k)
		if err != nil {
			panic(err)
		}
		if c.MatchString(newStr) {
			res = c.ReplaceAll([]byte(newStr), []byte(v))
			newStr = string(res)
		}
	}

	return newStr
}

// 解析翻译JSON字符串
func analysisJson() JSON {
	jsonStr := readFileTran("translate.json")
	var data JSON
	if err := json.Unmarshal([]byte(jsonStr), &data); err == nil {
		return data
	} else {
		fmt.Println(err)
		return nil
	}
}

// 获取所有文件
func getAllFile() {
	dir, _ := os.Getwd()
	err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
		var typ string
		if (info.Name() == "vendor" || info.Name() == ".git") && info.IsDir() {
			return filepath.SkipDir
		}
		if info.IsDir() {
			typ = "Dir"
		} else {
			typ = "File"
		}
		fmt.Println(info.Name(), typ)
		//fmt.Printf("[%s] %s\n", typ, path)
		return nil
	})
	if err != nil {
		panic(err)
	}
}

func FileOperaiton() {
	//ar1 := [...]int{9: -1}
	//sliceL := []string{"12", "123", "qwr", "123"}
	//fmt.Println(ar1)
	//sliceL = append(sliceL, "999", "ooo")
	//fmt.Println(sliceL, len(sliceL), cap(sliceL))
	//fmt.Println(sliceL[:7])
	values := []string{"12", "34", "78"}
	sum(values...)
}

type Point struct {
	X, Y float64
}

type any interface{}

func sum(values ...string) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()
	panic("how2j")
	fmt.Println(values)
}

func stydyGo() {
	type Movie struct {
		Title  string
		Year   int      `json:"released"`
		Color  bool     `json:"color,omitempty"`
		Actors []string `json:"actorsValue"`
	}

	movies := []Movie{
		{Title: "move1", Year: 2001, Color: true, Actors: []string{"12", "23"}},
		{Title: "move2", Year: 2002, Color: true, Actors: []string{"34", "45"}},
	}
	data, err := json.MarshalIndent(movies, "", "   ")
	if err != nil {
		log.Fatalf("JSON marshaling failed: %s", err)
	}
	fmt.Printf("%s\n", data)
}
